package com.mcrmb;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by www on 2015/2/11.
 */
public class Misc {

    private Map<String,Object> CardStatus =new HashMap<String,Object>();

    public Misc(){
        CardStatus.put("0", "卡异常，联系服主！");
        CardStatus.put("100", "卡异常，联系服主！");
        CardStatus.put("101", "卡异常，联系服主！");
        CardStatus.put("102", "卡异常，联系服主！");
        CardStatus.put("103", "卡异常，联系服主！");
        CardStatus.put("104", "卡异常，联系服主！");
        CardStatus.put("105", "卡异常，联系服主！");
        CardStatus.put("106", "卡异常，联系服主！");
        CardStatus.put("200", "卡密处理中！");
        CardStatus.put("201", "卡密不正确！");
        CardStatus.put("202", "不支持这种卡！");
        CardStatus.put("203", "处理异常，联系服主！");
        CardStatus.put("204", "不支持这种面值的卡！");
        CardStatus.put("205", "处理异常，联系服主！");
        CardStatus.put("206", "运营商维护中！");
        CardStatus.put("208", "卡已经被使用过了！");
        CardStatus.put("209", "卡提交过于频繁！");
        CardStatus.put("210", "处理异常，联系服主！");
        CardStatus.put("300", "卡已经处理成功！");
        CardStatus.put("302", "卡密处理失败！");
        CardStatus.put("303", "卡号无效！");
        CardStatus.put("304", "卡密错误！");
        CardStatus.put("305", "卡上余额不足！");
        CardStatus.put("306", "卡的状态错误！");
    }

    public String CardStatus(String status){//根据卡的状态码获取相应状态
        //Long start = System.nanoTime();
        try{
            String statusmsg = CardStatus.get(status).toString();
            return statusmsg;
        }catch (Exception e){
            return "卡密有误,请检查!";
        }
        //System.out.println("CardStatus:"+(System.nanoTime()-start));

    }

    public String StampToTime(String Stamp){//时间戳转日期函数
        //Long start = System.nanoTime();
        Long S = (new Long(Stamp))*1000;
        String Time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(S));
        //System.out.println("StampToTime:"+(System.nanoTime()-start));
        return Time;
    }

    public String getMD5(String str){//MD5函数!
        /*String key = PointSystem.plugin.getConfig().getString("key");
        System.out.print("key是这个。。。" + key);
        str+= key;
        System.out.print("STR是这个。。。" + str);*/
        //String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuilder buf = new StringBuilder("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            String re_md5 = buf.toString();
            return re_md5.toLowerCase();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }

}
